﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;

namespace PeisongWeb
{
    public partial class seller_customerEdit : customerPage
    {
        protected int customerId
        {
            get
            {
                int tmp;
                try { tmp = Convert.ToInt32(ViewState["customerId"]); }
                catch { tmp = 0; }
                return tmp;
            }
            set
            { ViewState["customerId"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (logic.customer.isAdmin())
                {

                    if (Request.QueryString["customer"] != null)
                    {
                        if (Library.Number.IsIntPositive(Request.QueryString["customer"]))
                        {
                            customerId = Convert.ToInt32(Request.QueryString["customer"]);
                        }
                    }
                    int userid = logic.customer.userId;
                    //string type = logic.customer.select("customerid=" + userid, "customerId", 1, 1).Tables[1].Rows[0]["type"].ToString();

                    //if (type == "1")
                    //{
                    //    menu1.menucss = "5";
                    //}
                    //else if (type == "2")
                    //{
                    //    menu1.menucss = "6";
                    //}
                    //else if (type == "3")
                    //{
                    //    menu1.menucss = "2";
                    //}
                    menu1.menucss = "1";
                    if (customerId != 0)
                    {
                        getDetails();
                        txt_Username.ReadOnly = true;
                    }
                }
                else
                {
                    Response.Write("<script lanuage=javascript>location='javascript:history.go(-1)'</script>");
                }

            }

        }
        private void getDetails()
        {
            CustomerEntity cus = logic.customer.getById(customerId);
            txt_Username.Text = cus.Username;
            txt_Username.Enabled = false;
            hid_Pwd.Value = cus.Password;

            li_pwd.Visible = false;
            li_pwd2.Visible = false;
            hid_complanyid.Value = cus.CompanyId.ToString();

            txt_Realname.Text = cus.Realname;
            txt_Phone.Text = cus.Phone;
            txt_Email.Text = cus.Email;
            //chx_isAdmin.Checked = cus.IsAdmin;
            chx_isRefuse.Checked = cus.IsRefuse;
            hid_expireDt.Value = cus.expireDt.ToString();
            hid_isAdmin.Value = cus.IsAdmin.ToString();
        }
        protected void btn_Save_Click(object sender, EventArgs e)
        {
            if (customerId == 0)
            {
                if (logic.customer.isExistUser(txt_Username.Text))
                {
                    Library.Script.ClientMsg("此用户名已经存在，请重新输入！");
                    return;
                }
            }
            CustomerEntity cus = new CustomerEntity();

            cus.Username = txt_Username.Text;
            cus.Email = txt_Email.Text;
            cus.Phone = txt_Phone.Text;
            cus.Realname = txt_Realname.Text;
            cus.IsRefuse = chx_isRefuse.Checked;
            int companyId = 0;
            if (hid_complanyid.Value == "")
            {
                companyId = logic.customer.getById(logic.customer.userId).CompanyId;
            }
            else
            {
                companyId = Convert.ToInt32(hid_complanyid.Value);
            }

            if (logic.company.getById(companyId).buyerType == "2")
            {
                hid_expireDt.Value = DateTime.Now.AddDays(7).ToString();
            }
            else
            {
                hid_expireDt.Value = DateTime.Now.ToString();
            }
            cus.CompanyId = companyId;
            cus.expireDt = Convert.ToDateTime(hid_expireDt.Value);
            if (customerId != 0)
            {
                cus.CustomerId = customerId;
                cus.Password = hid_Pwd.Value;
                cus.IsAdmin = Convert.ToBoolean(hid_isAdmin.Value);
                logic.customer.update(cus);
            }
            else
            {
                cus.IsAdmin = false;
                cus.Password = Library.StringLbl.EncryptMD5(txt_Pwd.Text);
                logic.customer.insert(cus);
            }
            Library.Script.ClientMsgUrl("保存成功！", "seller_customerList.aspx");

        }
    }
}
